#!/usr/bin/env python
""" AnyBackup is a windows specific backup program to keep multiple volumes in sync with multiple volumes
    Copyright (C) 2011  Andrew Hopkinson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Exported Classes:

    file -- a core AnyBackup structure, contains all meta info about a file, a list of files which this file contains (if it's a directory) and a link to the file's parent directory (unless it is the root file)
    """

import re
import os

class file:
    """ a core AnyBackup structure, contains all meta info about a file, a list of files which this file contains (if it's a directory) and a link to the file's parent directory (unless it is the root file) """
    def __init__(self, name, path, root, isDir, size, mtime, volName, serial, files = None):
        self._name = name
        self._path = path
        self._root = root
        self._isDir = isDir
        self._size = size
        self._volName = volName
        self._serial = serial
        self._files = files
        self._parent = None
        self._mtime = mtime
        
    def getMTime(self):
        try:
            return self._mtime
        except AttributeError:
            return None
    
    def setMTime(self,time):
        self._mtime = time

    def setPath(self,path):
        self._path = path

    def getPath(self):
        return self._path

    def setRoot(self,root):
        self._root = root

    def getRoot(self):
        if not self._root:
            self._root = self._volName + ':'
        return self._root

    def getName(self):
        return self._name

    def setName(self, name):
        self._name = name

    def getParent(self):
        return self._parent

    def setParent(self, parent):
        self._parent = parent

    def getSerial(self):
        return self._serial

    def setSerial(self, serial):
        self._serial = serial

    def getDir(self):
        if not self._path:
            return self.getRoot()
        return self.cleanDir(os.path.join(self.getRoot()+os.sep+self._path))
    
    def cleanDir(self,dir):
        return dir.replace('\\\\','\\')

    def setDir(self, dir):
        self._dir = dir

    def isDir(self):
        return self._isDir

    def setIsDir(self, isDir):
        self._isDir = isDir

    def getSize(self):
        return self._size

    def setSize(self, size):
        self._size = size

    def getVolName(self):
        return self._volName

    def setVolName(self, volName):
        self._volName = volName

    def getFiles(self):
        return self._files

    def getFileList(self):
        dirs = []
        files = []
        if not self._files:
            return files
        for fileRef in self._files:
            if fileRef.isDir():
                dirs.append(fileRef)
            else:
                files.append(fileRef)
        for dir in dirs:
            files += dir.getFileList()
        return files

    def setFiles(self, files):
        self._files = files

    def addFile(self, fileRef):
        result = False
        if not self._files:
            self._files = []
        if fileRef.getDir() == self.getDir()+self.getName():
            self._files.append(fileRef)
            return True
        for i in self._files:
            if i.isDir():
                result = i.addFile(fileRef)
        return result

    def search(self, regex):
        results = []
        reObj = re.compile(regex,re.IGNORECASE)
        for fileRef in self._files:
            if fileRef.isDir():
                results += fileRef.search(regex)
            if re.search(reObj,fileRef.getName()):
                results.append(fileRef)
        return results

    def findDir(self, dir):
        if dir == self.getPath():
            return True
        if re.search(dir,self.getPath(),re.IGNORECASE):
            return True
        if self.isDir():
            for fileRef in self._files:
                if fileRef.isDir:
                    if fileRef.findDir(dir):
                        return True
        return False


